# _*_ coding: utf-8 _*_
# @Time : 2024-01-15 21:55
# @Author : wf
import urllib.request as request
import urllib.parse

base_url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'

# 起决定的是 Cookie
headers = {
    # 'Accept': '*/*',
    # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Acs-Token': '1705316844306_1705329128787_Kj9V82oVpAn6dJTJI/G7aE3/cvHkj/EXqc6j2UieJIejuU/z81dPySSP6xsSLpkEhQ0v/R2TyVA85f04LHlEwYGmugTL59kkAt62VTPDzy/Bz/UMvE1NlAcyfWHMQf+13i6NV6ewGJuQArsn5J8tbOyig1m4irFNbg/Jg3vRStwX4cam99iW0Texury6ds3HtcM1aD6yxSwe8FpF0asK9asxjgu5YSwV5KYwa2HQBl6HLHygRBSWCqqGvKOmTsfchqxHy6PP4BUKb/0XhIEHcc3CCImx3AExZkRjaEmXRcGinLfdrNB5xMnZbZfjTPPTi4eXoV2jpwJQCTDrT/6gzN+IspJ8fp3NmIVm1VK+IANpdBuE7ybji0rxVrTLb9rf7fNO7zjw+9HVDE73RF12Tt3OZiGXS+lGf40qwvyZjpXOLCJ1qgmU8PCThnD76sfdUWUUT5s0MZbb+MMR7CppcvbbTzL5pJ2dAp+o5VPwp2IHUop+UhHAkDkWFG4Ycr//',
    # 'Connection': 'keep-alive',
    # 'Content-Length': '136',
    # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'BIDUPSID=9203607446FE9EAD526E9AF1D5C1D8C0; PSTM=1623154582; REALTIME_TRANS_SWITCH=1; SOUND_SPD_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=NsLXJmSUxxTXRuUjg5czJ1b1NZTkdYTjBHbEg0M2tUNnhaU1lkWlZSRm9iQlJrRVFBQUFBJCQAAAAAAAAAAAEAAADs4A0l0fS54rK70OK1xLCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGjf7GNo3-xjaV; BDUSS_BFESS=NsLXJmSUxxTXRuUjg5czJ1b1NZTkdYTjBHbEg0M2tUNnhaU1lkWlZSRm9iQlJrRVFBQUFBJCQAAAAAAAAAAAEAAADs4A0l0fS54rK70OK1xLCuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGjf7GNo3-xjaV; BAIDUID=749C06B51A5989485A1509F85B491C42:SL=0:NR=10:FG=1; MCITY=-131%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=749C06B51A5989485A1509F85B491C42:SL=0:NR=10:FG=1; ZFY=v4GrBxr7qFeO8LRLeYaV0WdN0fBQwCMg4W:Ax3sqHtFc:C; H_PS_PSSID=39996_40053_39938; BA_HECTOR=052ka08g008h8l04a105ala1on6nub1iqa8l41s; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=2; delPer=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1704978623,1705141768,1705239296,1705327869; ab_sr=1.0.1_MjEwMzM3M2VhOTRkZmQwYmNjYTFlZTEzYmM5NWM5OTRlOGFlZThjNjlkNDM4ZDI2YTdmMDc2NTExODMzYjVkMjc2MzAxYTFlMDI5YWI5NWQ3OWMyMGFhOGQyNDVkNjhkMzA4OTZjZDg3MTBiZThhN2UzZjQ0ZDExMmZjZTY3ZTg3NzY0YTI4ZDY2YWJmYzI0NjE4YWZjNWM2ODNkYWMyM2M3MGRhYTc0MGVhZjQxZThkYzQxZmM1NDdlNDFmZjkw; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1705329128',
    # 'Host': 'fanyi.baidu.com',
    # 'Origin': 'https://fanyi.baidu.com',
    # 'Referer': 'https://fanyi.baidu.com/translate?aldtype=16047&query=success%0D%0A&keyfrom=baidu&smartresult=dict&lang=auto2zh',
    # 'Sec-Fetch-Dest': 'empty',
    # 'Sec-Fetch-Mode': 'cors',
    # 'Sec-Fetch-Site': 'same-origin',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    # 'X-Requested-With': 'XMLHttpRequest',
    # 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    # 'sec-ch-ua-mobile': '?0',
    # 'sec-ch-ua-platform': '"Windows"',
}

# 正则表达式替换
# (.*): (.*)
# '\1': '\2',
data = {
    'from': 'en',
    'to': 'zh',
    'query': 'algorithm',
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '688464.942689',
    'token': '0c0d5a129a3fc468070703bba1e2b3d7',
    'domain': 'common',
    'ts': '1705330319886',
}
# post请求必须进行编码
data = urllib.parse.urlencode(data).encode('utf-8')

# 关键字传参
req = request.Request(base_url, data=data, headers=headers, method='POST')
response = request.urlopen(req)
content = response.read().decode("utf-8")
# print(content)

import json
print(json.loads(content))
